<?php

namespace app\common\model;

use think\Cache;

class AwardUser extends Base
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_award_user';

    /**
     * 操作时间
     * 获取器
     * @param string $value 当前字段数据
     * @param array $data 包含其他字段的数据
     * @return string
     */
    protected function getFortuneTimeAttr($value, $data)
    {
        return date('Y-m-d', json_decode($data['extend'], true)['award_time']);
    }

    /**获取档案所欲中奖的运势
     * @param array $map
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function get_award_fortune($map = [])
    {
        $uid = $map['uid'] ?? 0;
        $rid = $map['rid'] ?? 0;
        $type = $map['type'] ?? 0;
        $self_id = $map['self_id'] ?? 0;

        switch ($type)
        {
            case 1:
//                $se =['r.isself'=>1,'au.uid'=>$uid,'r.id'=>$rid];
                $where = ['au.uid' => $uid, 'au.aid' => ['in', config('goods.day_id')]];
                break;
            case 3:
                $where = ['au.uid' => $uid, 'au.aid' => ['in', config('goods.month_id')]];
                break;
            case 4:
                $where = ['au.uid' => $uid, 'au.aid' => ['in', config('goods.year_id')]];
                break;
        }

        if ($rid == $self_id['id'])
        {
            $where['au.rid'] = array(['=', 0], ['=', $self_id['id']], 'or');
        } else
        {
            $where['au.rid'] = $rid;
        }

        $prize = false;

        if ($this->cache_on >= 1)
        {
            $prize = Cache::get($uid . '_' . $rid . '_award_fortune_' . $type);
        }

        if ($prize === false)
        {
            // 获取中奖标识
            try
            {
                // 获取中奖标识
                $prize = $this
                    ->alias('au')
                    ->field('au.status,au.extend,g.expire_time,au.aid,au.id,au.rid')
                    ->join('wechat_backup_07_18_14_43_00.app_goods g', 'au.aid=g.id', 'INNER')
                    ->where($where)
                    ->select();

                $prize = json_decode(json_encode($prize, true), true);

                if ($this->cache_on >= 1)
                {
                    Cache::set($uid . '_' . $rid . '_award_fortune_' . $type, $prize, 86400 * 7);
                }

            }
            catch (\Exception $e)
            {
                $prize = [];
            }
            //更新用户赠送的rid，0为自己的档案id
            if ($prize)
            {
                foreach ($prize as $v)
                {
                    if (!$v['rid'] && $self_id)
                    {
                        $this->where('id', $v['id'])->update(['rid' => $self_id['id']]);
                    }
                }
            }
        }
        return $prize;
    }


    /**
     * 根据id获取中奖的相关信息
     * @param int $mid 赠送的运势id
     * @return array|bool|mixed|\PDOStatement|string|\think\Model
     */
    public function getAwardForId($mid = 0)
    {
        $data = false;

        if ($this->cache_on >= 1)
        {
            $data = Cache::get('award_fortune_' . $mid);
        }

        if ($data == false)
        {
            try
            {
                $data = $this->alias('au')
                    ->field('au.create_time,au.extend,au.status,u.username,u.nickname,u.sex,u.uen,u.id uid,u.tel,g.expire_time,au.rid')
                    ->join('wechat_backup_07_18_14_43_00.app_goods g', 'au.aid = g.id', 'LEFT')
                    ->join('wechat_backup_07_18_14_43_00.app_user u', 'au.uid = u.id', 'LEFT')
                    ->where('au.id', $mid)
                    ->findOrFail();
            }
            catch (\Exception $e)
            {
                $data = [];
            }

            if ($this->cache_on >= 1 && $data)
            {
                Cache::set('award_frotune_' . $mid, $data, 86400 * 7);
            }
        }
        return $data;
    }
}
